<?php
require_once 'classdb.php';
include("class.phpmailer.php");
include("class.smtp.php");
    class tin extends db
    {
        function laydanhmuchoa($id=-1)
        {
            return $this->get_by_thutu_db('sanpham_loai',$id,1);
        }
        function laylienhetructuyen()
        {
            return $this->get_by_thutu_db("lienhe");
        }
        function laychitietsanpham($id=-1)
        {
            return $this->get_by_thutu_db('sanpham', $id);
        }
        function laysanphammoiphantrang($pageNum=1,$pageSize=9,&$totalRows,$id=-1)
        {
            $totalRows = 0;
            $sql="select count(id) from sanpham
             where anhien=1 and (idloai='$id' or '$id'=-1)
             ORDER BY ngay desc";
            $kq = mysql_query($sql) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;

            $sql="select * from sanpham
                 where anhien=1 and (idloai='$id' or '$id'=-1)
                 ORDER BY ngay desc
                 LIMIT $startRow , $pageSize";
            $kq=mysql_query($sql) or die(mysql_error());
            //echo $sql; die;
            return $kq;
        }
        function laytintonghopphantrang($pageNum=1,$pageSize=9,&$totalRows,$id=-1,$idgroup='')
        {
            $totalRows = 0;
            $sql="select count(id) from tin_tonghop
             where anhien=1
             ORDER BY thutu asc";
            $kq = mysql_query($sql) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;

            $sql="select * from tin_tonghop
                 where anhien=1
                 ORDER BY thutu asc
                 LIMIT $startRow , $pageSize";
            $kq=mysql_query($sql) or die(mysql_error());
            //echo $sql; die;
            return $kq;
        }
        function laytintonghoplienquan($id,$idgroup='')
        {
             return $this->get_by_thutu_db("tin_tonghop", -1, 1, "and (idgroup='$idgroup' or '$idgroup'='') and (id between $id-3 and $id+3) and id<>$id","");
        }
        function layfield($idgroup='')
        {
            $sql="select * from field where idgroup='$idgroup' ";
            $kq=mysql_query($sql) or die(mysql_error());
            //echo $sql; die;
            return $kq;
        }
        function TongTienGioHang(){
		$sosp = count( $_SESSION['daySoLuong'] );
		 if ($sosp<=0) return 0;
		 reset( $_SESSION['daySoLuong'] );
		reset( $_SESSION['dayDonGia'] );
		 $tongtien = 0;
		 for ($i = 0; $i<$sosp ; $i++) {
			 $soluong = current( $_SESSION['daySoLuong'] );
			 $dongia = current( $_SESSION['dayDonGia'] );
			 $tongtien += $soluong*$dongia;
			 next( $_SESSION['daySoLuong'] );
			 next( $_SESSION['dayDonGia'] );
		   }//for
		return $tongtien;
	}

	function LuuDonHang(&$error){
	  //Tiếp nhận dữ liệu
	  $hoten = $_POST['hoten'];
          $diachi=$_POST['diachi'];
	  $dienthoai = $_POST['dienthoai'];
	  $ngaynhanhang = $_POST['ngaydangki'];
	  $noidung = $_POST['noidung'];

	  //kiểm tra dữ liệu
	  if (count($_SESSION['daySoLuong'])==0) {
		$error[] = "Bạn chưa chọn sản phẩm nào"; return false;
	  }

	  $hoten = trim(strip_tags($hoten));
	  $dienthoai = trim(strip_tags($dienthoai));
	  $ngaydangki = trim(strip_tags($ngaydangki));
	  $noidung = trim(strip_tags($noidung));

	  if (count($error)>0) return false;

	  if (get_magic_quotes_gpc()==false) {
		 $hoten = mysql_real_escape_string($hoten);
		 $dienthoai = mysql_real_escape_string($dienthoai);
		 $ngaydangki = mysql_real_escape_string($ngaydangki);
		 $noidung = mysql_real_escape_string($noidung);
	  }
          $Ngay_arr = explode("/",$ngaydangki); // array(17,11,2010)
		if (count($Ngay_arr)==3) {
			$d = $Ngay_arr[0]; //17
			$m = $Ngay_arr[1]; //11
			$y = $Ngay_arr[2]; //2010
			if (checkdate($m,$d,$y)==false) $ngaydangki = date("Y-m-d");
			else $ngaydangki = $y."-".$m."-".$d;
		}
		else $ngaydangki=date("Y-m-d");

	  //lưu dữ liệu vào db
	  $sessid = session_id();
	  $tongtien=$this->TongTienGioHang();
	  if (isset($_SESSION['id'])==false) {
		$sql="INSERT INTO donhang SET
			tennguoinhan = '$hoten', diachi = '$diachi',
			dtnguoinhan = '$dienthoai', ghichu = '$noidung',
			thoidiemnhanhang = '$ngaydangki', sessionid = '$sessid', TongTien='$tongtien' ";
			//echo $sql; die;
		$kq = mysql_query($sql) or die(mysql_error());
		$_SESSION['id'] = mysql_insert_id();
	  }else{
		$idDH = $_SESSION['id'];
		$sql="UPDATE donhang SET
			 tennguoinhan = '$hoten', diachi = '$diachi',
			dtnguoinhan = '$dienthoai', ghichu = '$noidung',
			thoidiemnhanhang = '$ngaydangki', sessionid = '$sessid' , TongTien='$tongtien'
			WHERE idDH = $idDH";
				//echo $sql; die;
		$kq = mysql_query($sql) or die(mysql_error());
	  }
	} //function LuuDonHang

	function LuuChiTietDonHang(){
	   $sosp = count($_SESSION['daySoLuong']);
	   if ($sosp<=0) {echo "Không có sản phẩm"; return;}
	   if (isset( $_SESSION['id'] )==false) {echo "Không có idDH"; return;}
	   $idDH = $_SESSION['id'];
	   $sql = "DELETE FROM donhangchitiet WHERE idDH = $idDH";
	   mysql_query($sql) or die(mysql_error());
	   reset( $_SESSION['daySoLuong'] );
	   reset( $_SESSION['dayDonGia'] );
	   reset( $_SESSION['dayTenSP'] );
	   reset( $_SESSION['dayGiamGia'] );
	   for ($i = 0; $i<$sosp ; $i++) {
		   $idSP = key( $_SESSION['daySoLuong'] );
		   $tensp = current( $_SESSION['dayTenSP'] );
		   $soluong = current( $_SESSION['daySoLuong'] );
		   $gia = current( $_SESSION['dayDonGia'] );
		   $giam = current( $_SESSION['dayGiamGia'] );
		   $sql ="INSERT INTO donhangchitiet (iddh, idsp, tensp, soluong, gia)
				  VALUES ($idDH, $idSP, '$tensp',$soluong, $gia)";
				  //echo $sql;die;
		   mysql_query($sql) or die(mysql_error());
		   next( $_SESSION['daySoLuong'] );
		   next( $_SESSION['dayDonGia'] );
		   next( $_SESSION['dayTenSP'] );
		   next( $_SESSION['dayGiamGia'] );
	   }//for
	}//function LuuChiTietDonHang

        function send_mail($fromname,$to,$body) {
	$mail = new PHPMailer();
	$mail->IsSMTP(); // set mailer to use SMTP
	$mail->Host = "smtp.gmail.com"; // specify main and backup server
	$mail->Port = 465; // set the port to use
	$mail->SMTPAuth = true; // turn on SMTP authentication
	$mail->SMTPSecure = 'ssl';
	$mail->Username = "conga0100@gmail.com"; // your SMTP username or your gmail username dia
	$mail->Password = "75321590"; // your SMTP password or your gmail password
	$from = ""; // Reply to this email
	//$to="lilyngoc.hoang9@gmail.com"; // Recipients email ID enquiry@vietnamtourism-jsc.com
	$name=""; // Recipient's name
	$mail->From = $from;
	$mail->FromName = $fromname; // Name to indicate where the email came from when the recepient received
	$mail->AddAddress($to,$name);
	//$mail->AddReplyTo($from,"Vo Duy Tuan");
	$mail->WordWrap = 50; // set word wrap
	$mail->IsHTML(true); // send as HTML
	$mail->Subject = "Khach hang lien he";
	//echo $body; die;
	$mail->Body = $body; //HTML Body
	$mail->AltBody = ""; //Text Body
	//$mail->SMTPDebug = 2;
	$mail->Send();
    }
 }

?>
